package com.share.finance.risk.rules.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.utils.IpUtil;
import com.share.finance.risk.common.model.sys.RuleNode;
import com.share.finance.risk.common.model.sys.RuleRequest;
import com.share.finance.risk.rules.mapper.ExecuteResultMapper;
import com.share.finance.risk.rules.model.db.ExecuteResult;
import com.share.finance.risk.rules.model.vo.ExecuteResultPageReq;
import com.share.finance.risk.rules.service.IExecuteResultService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ExecuteResultServiceImpl extends ServiceImpl<ExecuteResultMapper, ExecuteResult> implements IExecuteResultService {

    @Autowired
    private ExecuteResultMapper executeResultMapper;

    @Override
    public ExecuteResult queryLastDataByOrderIdAndProjectId(String orderId, Long projectId) {
        QueryWrapper<ExecuteResult> query = new QueryWrapper<>();
        query.eq("project_id", projectId);
        query.eq("order_id", orderId);
        query.orderByDesc("cycle_num");
        query.orderByAsc("salience");
        query.last("limit 1");//限定为1条,selectOne没有限定的功能
        ExecuteResult executeResult = executeResultMapper.selectOne(query);
        return executeResult;
    }

    @Override
    public int saveExecuteResult(RuleRequest req, ExecuteResult executeLog, RuleNode haltNode) {
        executeLog.setCreateTime(new Date());
        executeLog.setInputData(JSON.toJSONString(req));
        executeLog.setOrderId(req.getOrderId());
        executeLog.setCycleNum(req.getCycleNum());
        executeLog.setExeIp(IpUtil.getIp());
        executeLog.setSalience(haltNode.getSalience());
        return executeResultMapper.insert(executeLog);
    }

    @Override
    public Page<ExecuteResult> pageList(ExecuteResultPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<ExecuteResult> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getReqIp())) {
            queryWrapper.like(ExecuteResult::getReqIp, req.getReqIp());
        }
        if (req.getProjectId() != null) {
            queryWrapper.like(ExecuteResult::getProjectId, req.getProjectId());
        }
        if (StringUtils.isNotBlank(req.getInputData())) {
            queryWrapper.like(ExecuteResult::getInputData, req.getInputData());
        }
        if (StringUtils.isNotBlank(req.getOutputData())) {
            queryWrapper.like(ExecuteResult::getOutputData, req.getOutputData());
        }
        if (StringUtils.isNotBlank(req.getExeStatus())) {
            queryWrapper.like(ExecuteResult::getExeStatus, req.getExeStatus());
        }
        queryWrapper.orderByDesc(ExecuteResult::getId);
        List<ExecuteResult> list = executeResultMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }
}
